import store from '@/store'
function checkPermission(el, binding) {
	const { value } = binding
	let hasPermission = checkPermissionO(value)
	if (!hasPermission) {
		el.parentNode && el.parentNode.removeChild(el)
	}
}

export const checkPermissionO = (value) => {
	let flag = false
	const points = store.getters.permissions.data
	// 当传入的指令集为数组时
	if (value && value instanceof Array) {
		// 匹配对应的指令
		const hasPermission = points.some((point) => {
			return value.includes(point)
		})
		flag = !!hasPermission
		return flag
	} else {
		// eslint-disabled-next-line
		throw new Error('v-permission value is Array')
	}
}

export const permission = {
	bind(el, binding) {
		checkPermission(el, binding)
	},
	// 在绑定元素的父组件被挂载后调用
	inserted(el, binding) {
		checkPermission(el, binding)
	},
	// 在包含组件的 VNode 及其子组件的 VNode 更新后调用
	update(el, binding) {
		checkPermission(el, binding)
	}
}
